package cn.lp207663436.dynamicmanager.controller;


import javax.annotation.Resource;
import javax.sql.DataSource;

import cn.lp207663436.dynamicmanager.pojo.enumerate.SystemStatusEnum;
import cn.lp207663436.dynamicmanager.pojo.response.BaseResponse;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.BeanUtils;
import cn.lp207663436.dynamicmanager.pojo.request.UpdateCustomDataSourcePropertyRequest;
import cn.lp207663436.dynamicmanager.pojo.request.AddCustomDataSourcePropertyRequest;
import cn.lp207663436.dynamicmanager.pojo.request.GetCustomDataSourcePropertyListRequest;

import cn.lp207663436.dynamicmanager.service.CustomDataSourcePropertyService;
import cn.lp207663436.dynamicmanager.pojo.entity.CustomDataSourcePropertyEntity;


import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义数据源表 生产者控制器
 * </p>
 *
 * @author luopeng
 * @since 2023-07-04
 */
@RestController
@RequestMapping(value = "/dynamicmanager/customDataSourceProperty")
public class CustomDataSourcePropertyController {

    @Resource
    private CustomDataSourcePropertyService customDataSourcePropertyService;

    @Resource
    private DataSource dataSource;

    @PutMapping
    public BaseResponse updateCustomDataSourcePropertyInformation(@RequestBody UpdateCustomDataSourcePropertyRequest request) {

        CustomDataSourcePropertyEntity entity = customDataSourcePropertyService.getById(request.getId());
        if (entity == null) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
        }
        BeanUtils.copyProperties(request, entity);

        boolean flag = customDataSourcePropertyService.updateById(entity);
        if (flag) {
            customDataSourcePropertyService.removeDatasource(entity.getPoolName());
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            BeanUtils.copyProperties(entity, dataSourceProperty);
            customDataSourcePropertyService.addDatasource(dataSourceProperty);
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL);
        }
    }

    @DeleteMapping
    public BaseResponse deleteCustomDataSourceProperty(@RequestParam Long id) {

        customDataSourcePropertyService.removeById(id);
        return new BaseResponse(SystemStatusEnum.SUCCESS);
    }


    @PostMapping
    public BaseResponse addCustomDataSourceProperty(@RequestBody AddCustomDataSourcePropertyRequest request) {

        CustomDataSourcePropertyEntity entity = new CustomDataSourcePropertyEntity();
        BeanUtils.copyProperties(request, entity);

        boolean flag = customDataSourcePropertyService.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL);
        }
    }


    @GetMapping
    public BaseResponse getCustomDataSourcePropertyList(GetCustomDataSourcePropertyListRequest request) {

        List<CustomDataSourcePropertyEntity> entities = customDataSourcePropertyService.lambdaQuery()
                .likeRight(StringUtils.isNotBlank(request.getPoolName()), CustomDataSourcePropertyEntity::getPoolName, request.getPoolName())
                .ge(request.getStartTime() != null, CustomDataSourcePropertyEntity::getCreateTime, request.getStartTime())
                .le(request.getEndTime() != null, CustomDataSourcePropertyEntity::getCreateTime, request.getEndTime())
                .list();
        if (entities != null && entities.size() > 0) {
            if (request.getInit() != null) {
                DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
                Set<String> keys = ds.getDataSources().keySet();
                List<CustomDataSourcePropertyEntity> collectHasInit = entities.stream().filter(e -> keys.contains(e.getPoolName())).collect(Collectors.toList());

                if (request.getInit()) {
                    entities = collectHasInit;
                } else {
                    entities.removeAll(collectHasInit);
                }
            }
            BaseResponse success = new BaseResponse(SystemStatusEnum.SUCCESS);
            success.setData(entities);
            return success;
        } else {
            return new BaseResponse(SystemStatusEnum.DATANULL);
        }
    }

}
